package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.lite.R;
import com.wl156.model.WebServiceClient;

public class InputPassword extends BaseActivity implements OnClickListener {

	private EditText mMobile;
	private WebServiceClient mWebServiceClient;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_input_password);

		mWebServiceClient = new WebServiceClient(this);

		findViewById(R.id.confirm).setOnClickListener(this);
		mMobile = (EditText) findViewById(R.id.mobile);
	}

	@Override
	public void onClick(View v) {
		final String mobile = mMobile.getText().toString();
		if (TextUtils.isEmpty(mobile)) {
			Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
			return;
		}

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mProgressDialog;

			protected void onPreExecute() {
				mProgressDialog = ProgressDialog.show(InputPassword.this, "",
						"请稍后");
			};

			protected void onPostExecute(Object result) {
				mProgressDialog.dismiss();

				if (result instanceof Exception) {
					Exception e = (Exception) result;
					Toast.makeText(InputPassword.this, "网络异常，请稍后再试",
							Toast.LENGTH_SHORT).show();
				} else if (result instanceof SoapPrimitive) {
					SoapPrimitive sp = (SoapPrimitive) result;
					Toast.makeText(InputPassword.this, sp.toString(),
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(InputPassword.this, "发送成功，请等待接受短信验证码",
							Toast.LENGTH_SHORT).show();

					Intent i = new Intent(InputPassword.this,
							ResetPassword.class);
					i.putExtra("mobile", mobile);
					startActivity(i);
					finish();
				}
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient
							.common_GetMyVerificationCode(mobile);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();
	}
}
